//
//  WPYNavBar.m
//  KiddingBean
//
//  Created by Qianfeng on 16/2/26.
//  Copyright © 2016年 wangpengyu. All rights reserved.
//

#import "WPYNavBar.h"

@interface WPYNavBar ()
@property(nonatomic) CGFloat widthFloat;
@property(nonatomic, strong) UIView *btnLine;
@property (nonatomic) NSInteger currenIndex;
@property (nonatomic) NSInteger widthBtn;

@property (nonatomic, strong)NSMutableArray *LabelArray;


@end
@implementation WPYNavBar

- (instancetype)initWithFrame:(CGRect)frame titleColor:(UIColor *)titleColor andSelectTitleColor:(UIColor *)selectTitleColor {
    _currenIndex = 0;
    self.titleFont = [UIFont systemFontOfSize:17];
    self = [super initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width,frame.size.height)];
    self.backgroundColor = [UIColor whiteColor];
    self.titleColor = titleColor;
    self.selectColor = selectTitleColor;
    
    return self;
}


// 根据title 初始化tab

- (void)AddNavBarArray:(NSArray *)NavBarArray isBtnLine:(BOOL)isBtnLine btnLineColor:(UIColor *)color{
    NSInteger btnNum = NavBarArray.count;
    _widthBtn = (self.bounds.size.width)/(CGFloat)btnNum;
    
    self.LabelArray = [NSMutableArray new];
    for (int i = 0; i < btnNum; i++) {

        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(i * _widthBtn, 0, _widthBtn, self.bounds.size.height - 8)];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = self.titleColor;
        label.font = self.titleFont;
        label.text = MYLocalizedString(NavBarArray[i], nil);
        [label setTag:i+100];
        label.userInteractionEnabled = YES;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeTab:)];
        [label addGestureRecognizer:tap];
      
        if (i==0 && isBtnLine) {
        
            _widthFloat = 20 ;
            _btnLine = [[UIView alloc] initWithFrame:CGRectMake(label.center.x - _widthFloat/2, self.bounds.size.height -8,_widthFloat, 2)];
            [_btnLine setBackgroundColor:color];
            [self addSubview:_btnLine];
            label.textColor = self.selectColor;
        }
        [self addSubview:label];
        [self.LabelArray addObject:label];
    }
    self.selectIndex = 0;
}


// 点击title  切换tab
- (void)changeTab:(UITapGestureRecognizer *)tap {
    
    NSInteger index = tap.view.tag-100;
    
    [self.delegate selectChangetoViewNum:index];
    
    self.selectIndex = index;

}


// 切换tab
- (void)setSelectIndex:(NSInteger)selectIndex {
    
    UILabel * lastLabel = self.LabelArray[_currenIndex];
    
    UILabel * label = self.LabelArray[selectIndex];
    
    if (_currenIndex != selectIndex) {
        lastLabel.textColor = self.titleColor;
        label.textColor = self.selectColor;
        
        [UIView animateWithDuration:0.1 animations:^{
            [_btnLine setFrame:CGRectMake(label.center.x - _widthFloat/2, self.bounds.size.height - 7, _widthFloat, 2)];
        }];
        _currenIndex = selectIndex;
        
    }
}






/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
